﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using PowerSong.SongDatabase;

    public partial class FontControl : UserControl
        {

        public delegate void FontChangedDelegate(string fontName, double fontSize, FontEffectType fontEffect);

        public event FontChangedDelegate FontChanged = null;

        public TextBlock AssociatedPreviewControl { get; set; }

        public string SelectedName
            {
            get { return GetFontName(); }
            set { SetFontName(value); }
            }

        public double SelectedSize
            {
            get { return GetFontSize(); }
            set { SetFontSize(value); }
            }

        public FontEffectType SelectedStyle
            {
            get { return GetFontStyle(); }
            set { SetFontStyle(value); }
            }

        public FontControl()
            {
            InitializeComponent();
            FontNameComboBox.DataContext = Fonts.SystemFontFamilies;
            }

        private void FontSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
            RaiseFontChanged();
            }

        private void StyleCheckBoxChanged(object sender, RoutedEventArgs e)
            {
            RaiseFontChanged();
            }

        private void FontNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
            RaiseFontChanged();
            }

        private void RaiseFontChanged()
            {

            // Extract properties
            var name = GetFontName();
            var size = GetFontSize();
            var style = GetFontStyle();

            // Change properties of associated control
            if (AssociatedPreviewControl != null)
                {
                AssociatedPreviewControl.FontFamily = new FontFamily(name);
                AssociatedPreviewControl.FontSize = size * 500;
                AssociatedPreviewControl.FontWeight = ((style & FontEffectType.Bold) == FontEffectType.Bold) ? FontWeights.Bold : FontWeights.Normal;
                AssociatedPreviewControl.FontStyle = ((style & FontEffectType.Italic) == FontEffectType.Italic) ? FontStyles.Italic : FontStyles.Normal;
                AssociatedPreviewControl.TextDecorations = ((style & FontEffectType.Underline) == FontEffectType.Underline) ? TextDecorations.Underline : null;
                }

            // Raise the event
            if (FontChanged != null)
                {
                FontChanged(name, size, style);
                }
            }

        private string GetFontName()
            {
            return FontNameComboBox.SelectedItem == null
                ? "Arial"
                : FontNameComboBox.SelectedItem.ToString();
            }

        private void SetFontName(string name)
            {

            }

        private double GetFontSize()
            {
            var selectedItem = (ComboBoxItem)FontSizeComboBox.SelectedItem;
            return selectedItem == null
                ? 0.05
                : double.Parse(selectedItem.Tag.ToString());
            }

        private void SetFontSize(double size)
            {
            }

        private FontEffectType GetFontStyle()
            {
            var fontStyle = FontEffectType.None;
            if (IsBoldCheckBox.IsChecked == true) fontStyle |= FontEffectType.Bold;
            if (IsItalicCheckBox.IsChecked == true) fontStyle |= FontEffectType.Italic;
            if (IsUnderlineCheckBox.IsChecked == true) fontStyle |= FontEffectType.Underline;
            return fontStyle;
            }

        private void SetFontStyle(FontEffectType style)
            {
            IsBoldCheckBox.IsChecked = (style & FontEffectType.Bold) == FontEffectType.Bold;
            IsItalicCheckBox.IsChecked = (style & FontEffectType.Italic) == FontEffectType.Italic;
            IsUnderlineCheckBox.IsChecked = (style & FontEffectType.Underline) == FontEffectType.Underline;
            }

        }

    }
